using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Threading;
using System.Xml.Serialization;


namespace MetX.Data
{
	[Serializable, XmlRoot(Namespace = "", IsNullable = false)]
	public class XlgSettings
	{
		static XmlSerializer xs = new XmlSerializer(typeof(XlgSettings));

		[XmlAttribute]
		public string Filename;
		[XmlAttribute]
		public string DefaultConnectionString;
		[XmlAttribute]
		public string DefaultProviderName;
		[XmlAttribute]
		public string DefaultParentNamespace;

		public xlgTokenList TokenList = new xlgTokenList();

		[XmlArray("Sources", Namespace = "", IsNullable = false), XmlArrayItem("Source", Namespace = "", IsNullable = false)]
		public List<xlgSource> Sources = new List<xlgSource>();

		[XmlIgnore]
		public System.Windows.Forms.Form GUI;

		public XlgSettings() { /* XmlSerilizer */ }

		public XlgSettings(System.Windows.Forms.Form GUI) { this.GUI = GUI; }

		public static XlgSettings FromXml(string xmldoc)
		{
			return (XlgSettings)xs.Deserialize(new StringReader(xmldoc));
		}

		public string OuterXml()
		{
			StringBuilder sb = new StringBuilder();
			using (StringWriter sw = new StringWriter(sb))
				xs.Serialize(sw, this);
			return sb.ToString();
		}

		public void Save()
		{
			File.WriteAllText(Filename, OuterXml());
		}

		public static XlgSettings Load(string Filename)
		{
			return FromXml(File.ReadAllText(Filename));
		}

		public void Generate(System.Windows.Forms.Form GUI)
		{
			int GenCount = 0;
			bool ErrorEncountered = false;
			Parallel.ForEach<xlgSource>(Sources, delegate(xlgSource CurrSource)
			{
				if (ErrorEncountered) return;

				if (CurrSource.Selected)
				{
					Interlocked.Increment(ref GenCount);
					if (ErrorEncountered) return;

					if (CurrSource.RegenerateOnly)
						CurrSource.GenState = CurrSource.Regenerate(GUI);
					else
						CurrSource.GenState = CurrSource.Generate(GUI);

					if (ErrorEncountered) return;

					if (CurrSource.GenState == -1)
					{
						ErrorEncountered = true;
						CurrSource.GenState = -GenCount;
					}
					else
						CurrSource.GenState = GenCount;
				}
			});
			/*
			int GenCount = 0;
			foreach (xlgSource CurrSource in Sources)
			{
				if (CurrSource.Selected)
				{
					GenCount++;
					if(CurrSource.RegenerateOnly)
                        CurrSource.GenState = CurrSource.Regenerate(GUI);
                    else
						CurrSource.GenState = CurrSource.Generate(GUI);
					if (CurrSource.GenState == -1)
						CurrSource.GenState = -GenCount;
					else
						CurrSource.GenState = GenCount;
				}
			}
			*/
		}

		public void Regenerate(System.Windows.Forms.Form GUI)
		{
			int GenCount = 0;
			bool ErrorEncountered = false;
			Parallel.ForEach<xlgSource>(Sources, delegate(xlgSource CurrSource)
			{
				if (ErrorEncountered) return;
				if (CurrSource.Selected)
				{
					CurrSource.GenState = Interlocked.Increment(ref GenCount);
					if (ErrorEncountered) return;
					if (CurrSource.Regenerate(GUI) == -1)
						CurrSource.GenState = -CurrSource.GenState;
					if (ErrorEncountered) return;
				}
			});
			/*
						foreach (xlgSource CurrSource in Sources)
						{
							if (CurrSource.Selected)
							{
								LastGen = CurrSource.Regenerate(GUI);
								if (LastGen == -1)
									return -GenCount;
								GenCount++;
							}
						}
			*/
		}
	}

}
